/* eslint-disable max-lines */
import type {
  Slide,
  PPTTextElement,
  PPTImageElement,
  PPTElement,
} from '@/types/slides'
import { nanoid } from 'nanoid'

const HEAD_FIXED_KEY = {
  项目图片: 'projectImage',
  提案名称: 'proposalName',
  提案公司名称: 'proposalCompanyName',
  客户公司名称: 'customerCompanyName',
  部门: 'department',
  汇报人: 'reporter',
  日期: 'dateTime',
} as const

type FixedKeyType = keyof typeof HEAD_FIXED_KEY

function getHeadFixedKey(key: FixedKeyType): string {
  return HEAD_FIXED_KEY[key]
}

const logoElement = (): PPTImageElement => {
  return {
    type: 'image',
    id: nanoid(10),
    src: '',
    width: 161,
    height: 35,
    left: 829.1010646522004,
    top: 12.022788327929593,
    fixedRatio: true,
    rotate: 0,
    flipH: false,
    flipV: false,
  }
}
const backgroundElement = (): PPTImageElement => {
  return {
    type: 'image',
    id: nanoid(10),
    src: '',
    width: 1000,
    height: 346.3,
    left: 0,
    top: 216.2,
    fixedRatio: true,
    rotate: 0,
    flipH: false,
    flipV: false,
  }
}
const backgroundElement2 = (): PPTImageElement => {
  return {
    type: 'image',
    id: nanoid(10),
    src: '',
    width: 999.7680890538031,
    height: 562.5,
    left: 0,
    top: 0,
    fixedRatio: true,
    rotate: 0,
  }
}

const hasProjectImageShapeListElement = (): PPTElement[] => {
  return [
    {
      type: 'shape',
      id: nanoid(10),
      width: 172.62447429365446,
      height: 248.2623441601049,
      left: 0,
      top: 135.7762047321047,
      viewBox: [147.85070866141731, 223.68503937007873],
      path: ' M0,0 L147.85070866141731,0 L15.297165354330708,223.68503937007873 L0,223.68503937007873z',
      fill: '#0196E0',
      fixedRatio: false,
      rotate: 0,
      outline: { color: '#09111e', width: 0, style: 'solid' },
      text: {
        content: '',
        defaultFontName: '',
        defaultColor: '#333',
        align: 'middle',
      },
      flipH: false,
      flipV: false,
      special: true,
    },
    {
      type: 'shape',
      id: nanoid(10),
      width: 643.3,
      height: 248.3,
      left: 356.7,
      top: 135.8,
      viewBox: [610, 223.68503937007873],
      path: ' M132.5536220472441,0 L610,0 L610,223.68503937007873 L0,223.68503937007873z',
      fill: '#0196E0',
      fixedRatio: false,
      rotate: 0,
      outline: { color: '#09111e', width: 0, style: 'solid' },
      text: {
        content: '',
        defaultFontName: '',
        defaultColor: '#333',
        align: 'middle',
      },
      flipH: false,
      flipV: false,
      special: true,
    },
    {
      type: 'line',
      id: nanoid(10),
      width: 3,
      left: 524.7000000000002,
      top: 119.3408963585434,
      start: [0, 0],
      end: [611.4621006682877, 0],
      style: 'solid',
      color: '#A5ADFC',
      points: ['', ''],
    },
    {
      type: 'line',
      id: nanoid(10),
      width: 3,
      left: 369.9349268830751,
      top: 117.9282089166361,
      start: [0, 286.25774168869833],
      end: [155.6959004338451, 0],
      style: 'solid',
      color: '#A5ADFC',
      points: ['', ''],
    },
    {
      type: 'line',
      id: nanoid(10),
      width: 3,
      left: -286.2244008714597,
      top: 403.6408963585434,
      start: [0, 0],
      end: [658.0274548554598, 0],
      style: 'solid',
      color: '#A5ADFC',
      points: ['', ''],
    },
  ]
}
const defaultShapeListElement = (): PPTElement[] => {
  return [
    {
      type: 'shape',
      id: nanoid(10),
      left: 0,
      top: 136.7,
      width: 1000,
      height: 213.1,
      viewBox: [200, 200],
      path: 'M 0 0 L 200 0 L 200 200 L 0 200 Z',
      fill: 'rgba(1,150,224,1)',
      fixedRatio: false,
      rotate: 0,
    },
    {
      type: 'shape',
      id: nanoid(10),
      left: 130.963096309631,
      top: 104.76728272864156,
      width: 738.073807380738,
      height: 268.6858633799076,
      viewBox: [200, 200],
      path: 'M 0 0 L 200 0 L 200 200 L 0 200 Z',
      fill: '#00000000',
      fixedRatio: false,
      rotate: 0,
      outline: { width: 2, color: 'rgba(68,114,196,1)', style: 'solid' },
    },
    {
      type: 'line',
      id: nanoid(10),

      left: 0,
      top: 557.5,
      start: [0, 0],
      end: [999.7680890538032, 0],
      points: ['', ''],
      color: 'rgba(68,114,196,1)',
      style: 'solid',
      width: 5,
    },
    {
      type: 'line',
      id: nanoid(10),
      left: 0,
      top: 522.423273657289,
      start: [0, 0],
      end: [13.594815396514225, 0],
      points: ['', ''],
      color: 'rgba(68,114,196,1)',
      style: 'solid',
      width: 20,
    },
    {
      type: 'line',
      id: nanoid(10),
      left: 986.1732736572889,
      top: 521.4289869300043,
      start: [0, 0],
      end: [13.594815396514225, 0],
      points: ['', ''],
      color: 'rgba(68,114,196,1)',
      style: 'solid',
      width: 20,
    },
  ]
}

const hasProjectImageTextListElement = (): PPTTextElement[] => {
  return [
    {
      type: 'text',
      id: nanoid(10),
      left: 500,
      top: 185.42994757835217,
      width: 460.00755959178207,
      height: 72.171875,
      content: `<p style="text-align: justify;"><span style="font-size: 32px;"><span style="font-family: 等线;"><span style="color: rgb(255, 255, 255);">{{${getHeadFixedKey('提案名称')}}}</span></span></span></p>`,
      rotate: 0,
      defaultFontName: 'Microsoft Yahei',
      defaultColor: '#333',
      vertical: false,
    },
    {
      type: 'text',
      id: nanoid(10),
      left: 515.5,
      top: 221.4,
      width: 342.8,
      height: 50,
      content: `<p style="text-align: justify;"><span style="font-size: 10px;"><span style="font-family: 等线;"><span style="color: rgb(255, 255, 255);">{{${getHeadFixedKey('客户公司名称')}}}</span></span></span></p>`,
      rotate: 0,
      defaultFontName: 'Microsoft Yahei',
      defaultColor: '#333',
      vertical: false,
      wordSpace: 3,
    },
    {
      type: 'text',
      id: nanoid(10),
      left: 506.4499484004128,
      top: 298.65912274292884,
      width: 400,
      height: 49.984375,
      content: `<p style="text-align: justify;" class="ProseMirror-selectednode" draggable="true"><span style="font-size: 12px;"><span style="font-family: 等线;"><span style="color: rgb(255, 255, 255);">{{${getHeadFixedKey('汇报人')}}} {{${getHeadFixedKey('日期')}}}</span></span></span></p>`,
      rotate: 0,
      defaultFontName: 'Microsoft Yahei',
      defaultColor: '#333',
      vertical: false,
      wordSpace: 3,
    },
    {
      type: 'text',
      id: nanoid(10),
      left: 506.4499484004128,
      top: 329.05417389220963,
      width: 234.15351455644594,
      height: 49.984375,
      content: `<p style="text-align: justify;"><span style="font-size: 12px;"><span style="font-family: 等线;"><span style="color: rgb(255, 255, 255);">{{${getHeadFixedKey('提案公司名称')}}}{{${getHeadFixedKey('部门')}}}</span></span></span></p>`,
      rotate: 0,
      defaultFontName: 'Microsoft Yahei',
      defaultColor: '#333',
      vertical: false,
      wordSpace: 3,
    },
  ]
}

const defaultTextListElement = (): PPTTextElement[] => {
  return [
    {
      type: 'text',
      id: nanoid(10),
      left: 202.97029702970303,
      top: 195.44745430809394,
      width: 594.0594059405939,
      height: 94.484375,
      content: `<p style=""><span style="font-size: 48px;"><span style="font-family: Arial;"><span style="color: #ffffff;">{{${getHeadFixedKey('提案名称')}}}</span></span></span></p>`,
      rotate: 0,
      defaultFontName: 'Microsoft Yahei',
      defaultColor: '#333',
      vertical: false,
    },
    {
      type: 'text',
      id: nanoid(10),
      left: 213.59369474893919,
      top: 169.82245430809394,
      width: 182.991513437058,
      height: 50.828125,
      content: `<p style=""><span style="font-family: Arial;"><span style="color: #ffffff;">{{${getHeadFixedKey('客户公司名称')}}}</span></span></p>`,
      rotate: 0,
      defaultFontName: 'Microsoft Yahei',
      defaultColor: '#333',
      vertical: false,
    },
    {
      type: 'text',
      id: nanoid(10),
      left: 424.32869161053765,
      top: 389.601410855842,
      width: 250,
      height: 50,
      content: `<p style="" class="ProseMirror-selectednode" draggable="true"><span style="font-size: 14px;"><span style="font-family: Arial;"><span style="color: rgb(68, 114, 196);">{{${getHeadFixedKey('汇报人')}}}</span></span></span></p>`,
      rotate: 0,
      defaultFontName: 'Microsoft Yahei',
      defaultColor: '#333',
      vertical: false,
    },
    {
      type: 'text',
      id: nanoid(10),
      left: 391.5851786205227,
      top: 427.53350962661864,
      width: 250,
      height: 49.984375,
      content: `<p style="" class="ProseMirror-selectednode" draggable="true"><span style="font-size: 14px;"><span style="font-family: Arial;"><span style="color: rgb(68, 114, 196);">{{${getHeadFixedKey('提案公司名称')}}}{{${getHeadFixedKey('部门')}}}</span></span></span></p>`,
      rotate: 0,
      defaultFontName: 'Microsoft Yahei',
      defaultColor: '#333',
      vertical: false,
    },
    {
      type: 'text',
      id: nanoid(10),
      left: 396.6626571822799,
      top: 464.42898693000427,
      width: 250,
      height: 49.984375,
      content: `<p style="" class="ProseMirror-selectednode" draggable="true"><span style="font-size: 14px;"><span style="font-family: Arial;"><span style="color: rgb(68, 114, 196);">完成日期：{{${getHeadFixedKey('日期')}}}</span></span></span></p>`,
      rotate: 0,
      defaultFontName: 'Microsoft Yahei',
      defaultColor: '#333',
      vertical: false,
    },
  ]
}

const imageElement = (src: string): PPTImageElement => {
  return {
    src,
    type: 'image',
    id: nanoid(10),
    width: 489.45982806262253,
    height: 248.26234416010493,
    left: 0,
    top: 135.77620473210467,
    fixedRatio: true,
    rotate: 0,
  }
}

interface IHeadSlice {
  projectImage: string
  // proposalName: string;
  // proposalCompanyName: string;
  // customerCompanyName: string;
  // department: string;
  // reporter: string;
  // dateTime: string;
}

const hasProjectImageHeadSlice = (params: IHeadSlice): Slide => {
  const slide: Slide = {
    id: 'head1',
    elements: [],
    background: { type: 'solid', color: '#fff' },
  }
  // 注意插入顺序代表层级关系
  slide.elements.push(logoElement())
  slide.elements.push(backgroundElement())
  slide.elements.push(imageElement(params.projectImage))
  slide.elements.push(...hasProjectImageShapeListElement())
  slide.elements.push(...hasProjectImageTextListElement())

  return slide
}

const defaultHeadPage = (): Slide => {
  const slide: Slide = {
    id: nanoid(10),
    elements: [],
    background: { type: 'solid', color: '#fff' },
  }
  // 注意插入顺序代表层级关系
  slide.elements.push(logoElement())
  slide.elements.push(backgroundElement2())
  slide.elements.push(...defaultShapeListElement())
  slide.elements.push(...defaultTextListElement())

  return slide
}

export {
  FixedKeyType,
  getHeadFixedKey,
  hasProjectImageHeadSlice,
  defaultHeadPage,
}
